package com.easyjava.builder.strategy.impl;

import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.builder.BuildMapperXml;
import com.easyjava.builder.strategy.inf.MapperXmlStrategy;
import com.easyjava.utils.StringUtils;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 单条插入
 */
public class XmlFieldByKeyImpl implements MapperXmlStrategy {

    @Override
    public void generateMapperXml(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        Map<String, List<FieldInfo>> keyIndexMap = tableInfo.getKeyIndexMap();
        for (Entry<String, List<FieldInfo>> entry : keyIndexMap.entrySet()) {
            List<FieldInfo> fieldInfoList = entry.getValue();
            Integer index = 0;
            StringBuilder methodName = new StringBuilder();
            StringBuilder paramName = new StringBuilder();
            for (FieldInfo fieldInfo : fieldInfoList) {
                index++;
                String propertyName = fieldInfo.getPropertyName();
                String fieldName = fieldInfo.getFieldName();
                methodName.append(StringUtils.upperCaseFirst(propertyName));
                paramName.append(fieldName + "=#{" + propertyName + "}");
                if (index < fieldInfoList.size()) {
                    methodName.append("And");
                    paramName.append(" and ");
                }
            }
            String po = BuildMapperXml.getPo(tableInfo);
            String baseResultMap = BuildMapperXml.BASE_RESULT_MAP;
            String baseColumnList = BuildMapperXml.includeBaseColumnList();
            String tableName = tableInfo.getTableName();

            bw.write("\t<!--根据" + methodName + "查询-->\n");
            bw.write("\t<select id=\"selectBy" + methodName + "\" resultMap=\"" + baseResultMap + "\">\n");
            bw.write("\t\tselect " + baseColumnList + " from " + tableName + " where " + paramName + "\n");
            bw.write("\t</select>\n\n");

            bw.write("\t<!--根据" + methodName + "更新-->\n");
            bw.write("\t<update id=\"updateBy" + methodName + "\" parameterType=\"" + po + "\">\n");
            bw.write("\t\tupdate " + tableName + "\n");
            bw.write("\t\t<set>\n");
            for (FieldInfo fieldInfo : tableInfo.getFieldList()) {
                String propertyName = BuildMapperXml.INSERT_PREFIX + fieldInfo.getPropertyName();
                bw.write("\t\t\t<if test=\"" + propertyName + " != null\">\n");
                bw.write("\t\t\t\t" + fieldInfo.getFieldName() + " = #{" + propertyName + "},\n");
                bw.write("\t\t\t</if>\n");
            }
            bw.write("\t\t</set>\n");
            bw.write("\t\twhere " + paramName + "\n");
            bw.write("\t</update>\n\n");

            bw.write("\t<!--根据" + methodName + "删除-->\n");
            bw.write("\t<delete id=\"deleteBy" + methodName + "\">\n");
            bw.write("\t\tdelete from " + tableName + " where " + paramName + "\n");
            bw.write("\t</delete>\n\n");
        }
    }
}
